package com.gurid.service.sms.service.impl;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.gurid.common.base.result.ResultCode;
import com.gurid.service.base.exception.GuridException;
import com.gurid.service.sms.service.SmsService;
import com.gurid.service.sms.util.AliyunSmsUtils;
import com.gurid.service.sms.util.SmsProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SmsServiceImpl implements SmsService {

    private static final Logger log = LoggerFactory.getLogger(SmsServiceImpl.class);
    @Autowired
    private SmsProperties properties;

    @Override
    public void send(String mobile, String checkCode) throws ClientException {

        DefaultProfile profile = DefaultProfile.getProfile(properties.getRegionId(), properties.getKeyId(), properties.getKeySecrete());
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(properties.getEndpoint());
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", properties.getRegionId());
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", properties.getSignName());
        request.putQueryParameter("TemplateCode", properties.getTemplateCode());

        //request.putQueryParameter("TemplateParam", "{\"code\":\"" + checkCode + "\"}");
        Map<String, String> params = new HashMap<String, String>();
        params.put("Code", checkCode);
        Gson gson = new Gson();
        String json = gson.toJson(params);
        request.putQueryParameter("TemplateParam", json);

        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response.getData());

        //解析响应结果
        HashMap<String, String> map = gson.fromJson(response.getData(), HashMap.class);
        String code = map.get("Code");
        String message = map.get("Message");

        if ("isv.BUSINESS_LIMIT_CONTROL".equals(code)) {
            log.error("发送短信过于频繁" + "code-" + code + "message-" + message);
            throw new GuridException(ResultCode.SMS_SEND_ERROR_BUSINESS_LIMIT_CONTROL);
        }

        if (!"OK".equals(code)) {
            log.error("短信发送失败" + "code-" + code + "message-" + message);
            throw new GuridException(ResultCode.SMS_SEND_ERROR);
        }

    }

    public void send_new(String mobile, String checkCode) throws Exception {
        Map<String, String> params = new HashMap<String, String>();
        params.put("Code", checkCode);
        Gson gson = new Gson();
        String json = gson.toJson(params);

        SendSmsResponse response = AliyunSmsUtils.sendSms(
                properties.getKeyId(),
                properties.getKeySecrete(),
                properties.getEndpoint(),
                mobile,
                properties.getSignName(),
                properties.getTemplateCode(),
                json);

        //解析响应结果
        HashMap<String, String> map = gson.fromJson(String.valueOf(response.body), HashMap.class);
        String code = map.get("Code");
        String message = map.get("Message");

        if ("isv.BUSINESS_LIMIT_CONTROL".equals(code)) {
            log.error("发送短信过于频繁" + "code-" + code + "message-" + message);
            throw new GuridException(ResultCode.SMS_SEND_ERROR_BUSINESS_LIMIT_CONTROL);
        }

        if (!"OK".equals(code)) {
            log.error("短信发送失败" + "code-" + code + "message-" + message);
            throw new GuridException(ResultCode.SMS_SEND_ERROR);
        }
    }
}
